<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="search.supplier_name"
        clearable
        placeholder="请输入供应商名称"
      ></el-input>
      <el-select v-model="search.mode" placeholder="请选择经营模式">
        <el-option
          v-for="item in modeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.type" placeholder="请选择供应商类型">
        <el-option
          v-for="item in typeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.property" placeholder="请选择供应商性质">
        <el-option
          v-for="item in propertyList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width:300px;"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend">供应商</template>
        <el-button slot="append" icon="el-icon-search" @click="supplierBtn"></el-button>
      </el-input>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
      >查询</el-button>
      <el-button
        type="danger"
        size="small"
        icon="el-icon-delete"
        class="handleSearch"
        @click="empty()"
      >清空</el-button>
      <el-button
        class="add"
        size="small"
        type="primary"
        @click="addPermission"
        icon="el-icon-circle-plus"
      >添加商超供应商</el-button>
    </div>
    <el-table max-height="600" :data="tableData" border style="width: 100%" empty-text="暂无数据">
      <el-table-column prop="supplier_name" label="供应商名称" width="180" align="center"></el-table-column>
      <el-table-column prop="supplier_code" label="供应商编码" width="180" align="center"></el-table-column>
      <el-table-column prop="modeMsg" label="经营模式" align="center"></el-table-column>
      <el-table-column prop="statusMsg" label="状态" width="80" align="center"></el-table-column>
      <el-table-column prop="typeMsg" label="供应商类型" align="center"></el-table-column>
      <el-table-column prop="propertyMsg" label="供应商性质" align="center"></el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope)">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(scope)">删除</el-button>
          <el-button
            size="mini"
            icon="el-icon-edit-outline"
            type="warning"
            @click="binding(scope)"
          >商品绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
    <superSupplierDialog :dialog="dialog" @upData="getData"></superSupplierDialog>
    <supplierSearch :supplierdialog="supplierdialog" @tag="supplierTag"></supplierSearch>
    <goodsTogetherDialog :bindingDialog ='bindingDialog'></goodsTogetherDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { selectSupplierList, delSuppliers } from "@/api/supplier/supplierList";
import SuperSupplierDialog from "@/components/supplier/superSupplierDialog";
import SupplierSearch from "@/components/superGrossMargin/supplierSearch";
import GoodsTogetherDialog from "@/components/supplier/goodsTogetherDialog"
export default {
  components: {
    pagination: Pagination,
    superSupplierDialog: SuperSupplierDialog,
    supplierSearch: SupplierSearch,
    goodsTogetherDialog:GoodsTogetherDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      search: {
        supplier_name: "",
        supplier_code: "",
        mode: "",
        status: "",
        type: "",
        property: ""
      },
      modeList: [
        {
          value: 1,
          label: "经销"
        },
        {
          value: 2,
          label: "代销"
        },
        {
          value: 3,
          label: "联营"
        },
        {
          value: 4,
          label: "合营"
        },
        {
          value: 5,
          label: "租赁"
        },
        {
          value: 6,
          label: "扣率代销"
        },
         {
          value: 7,
          label: "购销"
        }
      ],
      statusList: [
        {
          value: 0,
          label: "正常"
        },
        {
          value: 1,
          label: "停用"
        },
        {
          value: 2,
          label: "冻结业务"
        },
        {
          value: 3,
          label: "冻结账款"
        }
      ],
      typeList: [
        {
          value: 1,
          label: "产地供应商"
        },
        {
          value: 2,
          label: "生产厂家"
        },
        {
          value: 3,
          label: "批发市场"
        },
        {
          value: 4,
          label: "种植基地"
        },
        {
          value: 5,
          label: "门店自采"
        },
        {
          value: 6,
          label: "其他"
        }
      ],
      propertyList: [
        {
          value: 1,
          label: "国有"
        },
        {
          value: 2,
          label: "民营"
        },
        {
          value: 3,
          label: "合资"
        },
        {
          value: 4,
          label: "外资"
        },
        {
          value: 5,
          label: "个体"
        }
      ],
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
        code:''

      },
      supplierdialog: {
        centerDialogVisible: false
      },
      bindingDialog: {
        centerDialogVisible: false,
        id: ""
      },
      supplierNameList: {
        name: "",
        code: ""
      }
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        supplier_name: this.search.supplier_name,
        supplier_code: this.supplierNameList.code,
        mode: this.search.mode,
        status: this.search.status,
        type: this.search.type,
        property: this.search.property
      };
      selectSupplierList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.paginate.data.forEach(result => {
              switch (result.mode) {
                case 1:
                  result.modeMsg = "经销";
                  break;
                case 2:
                  result.modeMsg = "代销";
                  break;
                case 3:
                  result.modeMsg = "联营";
                  break;
                case 4:
                  result.modeMsg = "合营";
                  break;
                case 5:
                  result.modeMsg = "租赁";
                  break;
                case 6:
                  result.modeMsg = "扣率代销";
                  break;
                  case 7:
                  result.modeMsg = "购销";
                  break;
              }
              switch (result.status) {
                case 0:
                  result.statusMsg = "正常";
                  break;
                case 1:
                  result.statusMsg = "停用";
                  break;
                case 2:
                  result.statusMsg = "冻结业务";
                  break;
                case 3:
                  result.statusMsg = "冻结账款";
                  break;
              }
              switch (result.type) {
                case 1:
                  result.typeMsg = "产地供应商";
                  break;
                case 2:
                  result.typeMsg = "生产厂家";
                  break;
                case 3:
                  result.typeMsg = "批发市场";
                  break;
                case 4:
                  result.typeMsg = "种植基地";
                  break;
                case 5:
                  result.typeMsg = "门店自采";
                  break;
                case 6:
                  result.typeMsg = "其他";
                  break;
              }
              switch (result.property) {
                case 1:
                  result.propertyMsg = "国有";
                  break;
                case 2:
                  result.propertyMsg = "民营";
                  break;
                case 3:
                  result.propertyMsg = "合资";
                  break;
                case 4:
                  result.propertyMsg = "外资";
                  break;
                case 5:
                  result.propertyMsg = "个体";
                  break;
              }
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.last_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true
      };
    },
    //添加
    addPermission() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
    },
    //绑定商品
    binding(scope) {
      this.bindingDialog.centerDialogVisible = true;
      this.bindingDialog.id = scope.row.id;
      this.bindingDialog.code =  scope.row.supplier_code
    },
    //编辑
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.id = scope.row.id;
    },
    //删除
    handleDelete(scope) {
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            id: scope.row.id
          };
          delSuppliers(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //清空
    empty() {
      this.search = {
        supplier_name: "",
        supplier_code: "",
        mode: "",
        status: "",
        type: "",
        property: ""
      };
      this.supplierNameList.name = "";
      this.supplierNameList.code = "";
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>